package ch.ffhs.bscinf2009.organizeanything.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lir
 * An organizable holds all information of a certain thing which can be organized, such as its name and its properties
 */
public class Organizable implements Serializable {
	private static final long serialVersionUID = -2726155598570544680L;
	long rowId;
	List<AbstractType> properties = new ArrayList<AbstractType>();
	String organizableName;

	@SuppressWarnings("javadoc")
	public void setOrganizableName(String organizableName) {
		this.organizableName = organizableName;
	}

	@SuppressWarnings("javadoc")
	public String getOrganizableName() {
		return organizableName;
	}

	@SuppressWarnings("javadoc")
	public void setProperties(List<AbstractType> properties) {
		this.properties = properties;
	}

	@SuppressWarnings("javadoc")
	public List<AbstractType> getProperties() {
		return properties;
	}

	@SuppressWarnings("javadoc")
	public long getRowId() {
		return rowId;
	}

	@SuppressWarnings("javadoc")
	public void setRowId(long l) {
		this.rowId = l;
	}
	
	/**
	 * copies the structure of the properties of the source organizable to the destination organizable. Needed to not break the properties of a root organizable. 
	 * @param destination where to copy the properties
	 * @param source where to copy the properties from
	 */
	public static void deepCopyProperties(Organizable destination, Organizable source) {
		if (destination.getProperties() == null) {
			destination.setProperties(new ArrayList<AbstractType>());
		}
		for (AbstractType a : source.getProperties()) {
			AbstractType b = null;
			if (a instanceof StringType) {
				b = new StringType();
			}
			if (a instanceof DateType) {
				b = new DateType();
			}
			if (a instanceof ValueChooserType) {
				b = new ValueChooserType();
				b.parseTypeDefinition(a.buildTypeDefinition());
			}
			b.setDescriptorValue(a.isDescriptorValue());
			b.setPropertyName(a.getPropertyName());
			destination.getProperties().add(b);
		}
	}
	
	@Override
	public String toString() {
		String blubberString = "";
		for (AbstractType t : getProperties()) {
			blubberString+=t.displayValue()+" / ";
		}
		return blubberString;
	}

}
